package ui.xmlVisualization;

import static preprocess.XMLContracter.ATTRIBUTE;
import static preprocess.XMLContracter.NESTING;
import static preprocess.XMLContracter.REPETITION;
import static preprocess.XMLContracter.TEXT;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.filechooser.FileFilter;

import org.dom4j.Document;
import org.dom4j.io.SAXReader;

import preprocess.XMLContracter;

/**
 * a simple control pane of XMLVisualization
 * 
 * @author huangcd
 * 
 */
public class Main extends JFrame
{

    private static final long serialVersionUID = 1L;
    private JPanel            jContentPane     = null;
    private JCheckBox         text             = null;
    private JCheckBox         attribute        = null;
    private JCheckBox         repetition       = null;
    private JCheckBox         nesting          = null;
    private JButton           select           = null;
    private JButton           show             = null;
    private File              selectFile       = null;

    /**
     * This is the default constructor
     */
    public Main()
    {
        super();
        initialize();
    }

    /**
     * This method initializes this
     * 
     * @return void
     */
    private void initialize()
    {
        setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 14)); // Generated
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Generated
        Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
        int y = (screen.height - 213) / 2;
        int x = (screen.width - 148) / 2;
        this.setLocation(x, y);
        this.setSize(148, 213);
        setResizable(false);
        setContentPane(getJContentPane());
        setTitle("XML显示");
        setVisible(true); // Generated
    }

    /**
     * This method initializes jContentPane
     * 
     * @return javax.swing.JPanel
     */
    private JPanel getJContentPane()
    {
        if (jContentPane == null)
        {
            jContentPane = new JPanel();
            jContentPane.add(getText(), null); // Generated
            jContentPane.add(getAttribute(), null); // Generated
            jContentPane.add(getRepetition(), null); // Generated
            jContentPane.add(getNesting(), null); // Generated
            jContentPane.add(getSelect(), null); // Generated
            jContentPane.add(getShow(), null); // Generated
        }
        return jContentPane;
    }

    /**
     * This method initializes text
     * 
     * @return javax.swing.JCheckBox
     */
    private JCheckBox getText()
    {
        if (text == null)
        {
            text = new JCheckBox();
            text.setText("约简文字"); // Generated
            text.setHorizontalTextPosition(SwingConstants.TRAILING); // Generated
            text.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 14)); // Generated
            text.setHorizontalAlignment(SwingConstants.CENTER); // Generated
        }
        return text;
    }

    /**
     * This method initializes attribute
     * 
     * @return javax.swing.JCheckBox
     */
    private JCheckBox getAttribute()
    {
        if (attribute == null)
        {
            attribute = new JCheckBox();
            attribute.setText("约简属性"); // Generated
            attribute.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 14)); // Generated
            attribute.setHorizontalAlignment(SwingConstants.CENTER); // Generated
        }
        return attribute;
    }

    /**
     * This method initializes repetition
     * 
     * @return javax.swing.JCheckBox
     */
    private JCheckBox getRepetition()
    {
        if (repetition == null)
        {
            repetition = new JCheckBox();
            repetition.setHorizontalAlignment(SwingConstants.CENTER); // Generated
            repetition.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 14)); // Generated
            repetition.setText("约简同名兄弟节点"); // Generated
        }
        return repetition;
    }

    /**
     * This method initializes nesting
     * 
     * @return javax.swing.JCheckBox
     */
    private JCheckBox getNesting()
    {
        if (nesting == null)
        {
            nesting = new JCheckBox();
            nesting.setText("约简同名父子节点"); // Generated
            nesting.setHorizontalAlignment(SwingConstants.CENTER); // Generated
            nesting.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 14)); // Generated
        }
        return nesting;
    }

    /**
     * This method initializes select
     * 
     * @return javax.swing.JButton
     */
    private JButton getSelect()
    {
        if (select == null)
        {
            select = new JButton();
            select.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 14)); // Generated
            select.setText("选择XML文件"); // Generated
            select.addActionListener(new java.awt.event.ActionListener()
            {
                private Main parent;

                private java.awt.event.ActionListener setParent(Main main)
                {
                    parent = main;
                    return this;
                }

                public void actionPerformed(java.awt.event.ActionEvent e)
                {
                    JFileChooser chooser = new JFileChooser();
                    chooser.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 14));
                    chooser.setFileFilter(new FileFilter()
                    {
                        @Override
                        public boolean accept(File f)
                        {
                            return f.getName().toLowerCase().endsWith(".xml")
                                    || f.isDirectory();
                        }

                        @Override
                        public String getDescription()
                        {
                            return "*.xml";
                        }
                    });
                    int result = chooser.showOpenDialog(null);
                    if (result == JFileChooser.APPROVE_OPTION)
                    {
                        parent.selectFile = chooser.getSelectedFile();
                    }
                }
            }.setParent(this));
        }
        return select;
    }

    /**
     * This method initializes show
     * 
     * @return javax.swing.JButton
     */
    private JButton getShow()
    {
        if (show == null)
        {
            show = new JButton();
            show.setFont(new Font("\u5b8b\u4f53", Font.PLAIN, 14)); // Generated
            show.setText("显示"); // Generated
            show.addActionListener(new java.awt.event.ActionListener()
            {
                private Main parent;

                private java.awt.event.ActionListener setParent(Main main)
                {
                    parent = main;
                    return this;
                }

                public void actionPerformed(java.awt.event.ActionEvent e)
                {
                    int tag = (text.isSelected() ? TEXT : 0)
                            | (attribute.isSelected() ? ATTRIBUTE : 0)
                            | (nesting.isSelected() ? NESTING : 0)
                            | (repetition.isSelected() ? REPETITION : 0);
                    try
                    {
                        if (parent.selectFile == null)
                        {
                            JOptionPane
                                    .showMessageDialog(parent, "请先指定一个XML文件");
                            return;
                        }
                        SAXReader reader = new SAXReader();
                        Document doc = reader.read(parent.selectFile);
                        XMLContracter contracter = new XMLContracter();
                        contracter.contract(doc, tag);
                        XMLTree.showTree(doc);
                    }
                    catch (Exception e2)
                    {
                        JOptionPane.showMessageDialog(parent, e2.getMessage(),
                                "程序运行出错", JOptionPane.ERROR_MESSAGE);
                    }
                }
            }.setParent(this));
        }
        return show;
    }

    public static void main(String[] args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                Main thisClass = new Main();
                thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                thisClass.setVisible(true);
            }
        });
    }

} // @jve:decl-index=0:visual-constraint="10,10"
